package com.dldata.devtools30.controller.zhibiao;

import com.dldata.devtools30.entity.*;
import com.dldata.devtools30.repository.metadata.DevtModelMetadataTableDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbcfgInfoEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanInfoEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanParaEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanParatjEntityDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/DevtZbplanParatjEntityController")
@Slf4j
public class DevtZbplanParatjEntityController {

    @Autowired
    DevtZbplanInfoEntityDao devtZbplanInfoEntityDao;
    
    @Autowired
    DevtZbplanParaEntityDao devtZbplanParaEntityDao;
    
    @Autowired
    DevtZbplanParatjEntityDao devtZbplanParatjEntityDao;
    
    @Autowired
    DevtModelMetadataTableDao devtModelMetadataTableDao;
    
    @Autowired
    DevtZbcfgInfoEntityDao devtZbcfgInfoEntityDao;
    
    @RequestMapping(value = "/getAllModelList")
    @ResponseBody
    public Result getAllModelList(@RequestParam Map<String, String> paramMap){
        String planid = paramMap.get("planid");
        List<DevtZbcfgInfoEntity> zbcfgInfoEntityList = devtZbcfgInfoEntityDao.getAllByPlanId(planid);
        Map<String, List<DevtZbcfgInfoEntity>> modelIdMap = zbcfgInfoEntityList.stream().collect(Collectors.groupingBy(DevtZbcfgInfoEntity::getModelid));
        
        Iterable<DevtModelMetadataTableEntity> modelMetadataTableDaoAllById = devtModelMetadataTableDao.findAllById(modelIdMap.keySet());
        List<DevtModelMetadataTableEntity> metadataTableEntities =Lists.newArrayList(modelMetadataTableDaoAllById);
        return Result.ok(metadataTableEntities);
    }
    
    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
        
        List<DevtZbplanParatjEntity> zbplanInfoEntityArrayList =devtZbplanParatjEntityDao.getAllByParaid(paramMap.get("paraid"));
    
        zbplanInfoEntityArrayList.sort(new Comparator<DevtZbplanParatjEntity>() {
            @Override
            public int compare(DevtZbplanParatjEntity o1, DevtZbplanParatjEntity o2) {
                if(o1.getXh()==null) o1.setXh(0);
                if(o2.getXh()==null) o2.setXh(0);
                return o1.getXh().compareTo(o2.getXh());
            }
        });
        
        return Result.ok(zbplanInfoEntityArrayList);
    }
    
    
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtZbplanParatjEntity entity){
        if(StringUtil.isEmptyOrLength0(entity.getTjsql())){
            return Result.error("条件不能为空");
        }
        
        if(StringUtil.isEmptyOrLength0(entity.getId())){
            entity.setId(UUID.randomUUID().toString());
        }
    
        DevtZbplanParaEntity devtZbplanParaEntity
                = devtZbplanParaEntityDao.findById(entity.getParaid()).orElse(new DevtZbplanParaEntity());
        if(devtZbplanParaEntity!=null){
            entity.setParaname(devtZbplanParaEntity.getParaname());
        }
    
        DevtModelMetadataTableEntity modelMetadataTableEntity
                = devtModelMetadataTableDao.findById(entity.getModelid()).orElse(new DevtModelMetadataTableEntity());
        if(modelMetadataTableEntity!=null){
            entity.setModecode(modelMetadataTableEntity.getCode());
            entity.setModename(modelMetadataTableEntity.getName());
        }
    
        DevtZbplanInfoEntity devtZbplanInfoEntity = devtZbplanInfoEntityDao.findById(entity.getPlanid()).orElse(new DevtZbplanInfoEntity());
        entity.setPlancode(devtZbplanInfoEntity.getPlancode());
    
        List<DevtZbplanParatjEntity> allByParaid = devtZbplanParatjEntityDao.getAllByParaid(entity.getParaid());
        for (int i = 0; i < allByParaid.size(); i++) {
            DevtZbplanParatjEntity devtZbplanParatjEntity = allByParaid.get(i);
            devtZbplanParatjEntity.setXh(i+1);
        }
        entity.setXh(allByParaid.size()+1);
    
        devtZbplanParatjEntityDao.save(entity);
        
        return Result.ok();
    }
    
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){
        
        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            DevtZbplanParatjEntity devtZbclassInfoEntity = devtZbplanParatjEntityDao.findById(id).orElse(null);
            if(devtZbclassInfoEntity!=null){
                devtZbplanParatjEntityDao.delete(devtZbclassInfoEntity);
            }
        }
        catch (Exception ex){
            log.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
    
    
    @RequestMapping("edit")
    public String toEdit(String id, Model model,String paraid,String planid){
        DevtZbplanParatjEntity  zbobjInfoEntity = new DevtZbplanParatjEntity();
        if(!StringUtil.isEmptyOrLength0(id)){
            zbobjInfoEntity = devtZbplanParatjEntityDao.findById(id).orElse(new DevtZbplanParatjEntity());
        }
        if(StringUtil.isEmptyOrLength0(zbobjInfoEntity.getPlanid())){
            zbobjInfoEntity.setPlanid(planid);
        }
        if(StringUtil.isEmptyOrLength0(zbobjInfoEntity.getParaid())){
            zbobjInfoEntity.setParaid(paraid);
            DevtZbplanParaEntity zbplanParaEntity = devtZbplanParaEntityDao.findById(paraid).orElse(new DevtZbplanParaEntity());
            if(StringUtil.isEmptyOrLength0(zbobjInfoEntity.getPlanid())){
                zbobjInfoEntity.setPlanid(zbplanParaEntity.getPlanid());
            }
        }
        model.addAttribute("entity",zbobjInfoEntity);
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoplanParaTjEdit";
    }
}
